﻿namespace WMS6.SQLServerDAL
{
    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Common;
    using System.Data.SqlClient;
    using WMS6.Common;
    using WMS6.DataEntity;
    using WMS6.DBUtility;
    using WMS6.IDAL;
    using WMS6.QueryEntity;

    public class GeneralLedgerDA : IGeneralLedgerDA
    {
        private string CREATED_BY = "@CREATED_BY";
        private string GENERAL_LEDGER_ID = "@GENERAL_LEDGER_ID";
        private string GENERAL_LEDGER_NAME = "@GENERAL_LEDGER_NAME";
        private string IS_ACTIVE = "@IS_ACTIVE";
        private string REMARK = "@REMARK";
        private string SQL_CHECK_GENERAL_LEDGER_ID_UNIQUE = " SELECT COUNT(1) FROM @_@GENERAL_LEDGER WHERE GENERAL_LEDGER_ID = @GENERAL_LEDGER_ID ";
        private string SQL_DELETE_GENERAL_LEDGER = " DELETE FROM @_@GENERAL_LEDGER WHERE GENERAL_LEDGER_ID = @GENERAL_LEDGER_ID ";
        private string SQL_INSERT_GENERAL_LEDGER = " INSERT INTO @_@GENERAL_LEDGER ( WH_ID, GENERAL_LEDGER_ID, GENERAL_LEDGER_NAME, IS_ACTIVE, REMARK, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE) VALUES ( @WH_ID, @GENERAL_LEDGER_ID, @GENERAL_LEDGER_NAME, @IS_ACTIVE, @REMARK, @CREATED_BY, GETDATE(), @UPDATED_BY, GETDATE() )  ";
        private string SQL_SELECT_ALL_GENERAL_LEDGER = " SELECT  ROWID, WH_ID, GENERAL_LEDGER_ID, GENERAL_LEDGER_NAME, TS, IS_ACTIVE, REMARK, CREATED_BY, CREATED_DATE, UPDATED_BY, UPDATED_DATE  FROM  @_@GENERAL_LEDGER ";
        private string SQL_UPDATE_GENERAL_LEDGER = " UPDATE @_@GENERAL_LEDGER SET WH_ID = @WH_ID, GENERAL_LEDGER_NAME = @GENERAL_LEDGER_NAME, IS_ACTIVE = @IS_ACTIVE, REMARK = @REMARK, UPDATED_BY = @UPDATED_BY, UPDATED_DATE = GETDATE() WHERE GENERAL_LEDGER_ID = @GENERAL_LEDGER_ID  ";
        private string TS = "@TS";
        private string UPDATED_BY = "@UPDATED_BY";
        private string WH_ID = "@WH_ID";

        public bool CheckGeneralLedgerIDUnique(string generalLedgerID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalLedgerID) };
            return (DBHelper.ExecuteScalar(CommandType.Text, this.SQL_CHECK_GENERAL_LEDGER_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public bool CheckGeneralLedgerIDUnique(DataBase dataBase, DbTransaction tran, string generalLedgerID, string whLoginID)
        {
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalLedgerID) };
            return (dataBase.ExecuteScalar(tran, CommandType.Text, this.SQL_CHECK_GENERAL_LEDGER_ID_UNIQUE.Replace("@_@", whLoginID), paras).ToString() == "1");
        }

        public int DeleteGeneralLedger(List<string> generalLedgerID, string whLoginID)
        {
            int result = 0;
            DataBase dataBase = new DataBase();
            DbConnection conn = dataBase.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                for (int j = 0; j < generalLedgerID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalLedgerID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_GENERAL_LEDGER.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int DeleteGeneralLedger(DataBase dataBase, DbTransaction tran, List<string> generalLedgerID, string whLoginID)
        {
            int result = 0;
            try
            {
                for (int j = 0; j < generalLedgerID.Count; j++)
                {
                    SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalLedgerID[j]) };
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_DELETE_GENERAL_LEDGER.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public DataSet GetAllGeneralLedger(string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_GENERAL_LEDGER.Replace("@_@", whLoginID);
            return DBHelper.ExecuteDataSet(CommandType.Text, sql);
        }

        public DataSet GetAllGeneralLedger(DataBase dataBase, DbTransaction tran, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_GENERAL_LEDGER.Replace("@_@", whLoginID);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, sql);
        }

        public GeneralLedgerInfo GetGeneralLedgerByID(string generalledgerID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_GENERAL_LEDGER.Replace("@_@", whLoginID) + " WHERE GENERAL_LEDGER_ID = @GENERAL_LEDGER_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalledgerID) };
            GeneralLedgerInfo generalLedgerInfo = null;
            using (IDataReader reader = DBHelper.ExecuteReader(CommandType.Text, sql, paras))
            {
                if (reader.Read())
                {
                    generalLedgerInfo = new GeneralLedgerInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["GENERAL_LEDGER_ID"].ToString(), reader["GENERAL_LEDGER_NAME"].ToString(), (byte[]) reader["TS"], reader["IS_ACTIVE"].ToString(), reader["REMARK"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
                }
            }
            return generalLedgerInfo;
        }

        public GeneralLedgerInfo GetGeneralLedgerByID(DataBase dataBase, DbTransaction tran, string generalledgerID, string whLoginID)
        {
            string sql = this.SQL_SELECT_ALL_GENERAL_LEDGER.Replace("@_@", whLoginID) + " WHERE GENERAL_LEDGER_ID = @GENERAL_LEDGER_ID  ";
            SqlParameter[] paras = new SqlParameter[] { new SqlParameter("@GENERAL_LEDGER_ID", generalledgerID) };
            GeneralLedgerInfo generalLedgerInfo = null;
            IDataReader reader = dataBase.ExecuteReader(tran, CommandType.Text, sql, paras);
            if (reader.Read())
            {
                generalLedgerInfo = new GeneralLedgerInfo((reader["ROWID"].ToString() != "") ? int.Parse(reader["ROWID"].ToString()) : 0, reader["WH_ID"].ToString(), reader["GENERAL_LEDGER_ID"].ToString(), reader["GENERAL_LEDGER_NAME"].ToString(), (byte[]) reader["TS"], reader["IS_ACTIVE"].ToString(), reader["REMARK"].ToString(), reader["CREATED_BY"].ToString(), (reader["CREATED_DATE"].ToString() != "") ? DateTime.Parse(reader["CREATED_DATE"].ToString()) : new DateTime(), reader["UPDATED_BY"].ToString(), (reader["UPDATED_DATE"].ToString() != "") ? DateTime.Parse(reader["UPDATED_DATE"].ToString()) : new DateTime());
            }
            if (!reader.IsClosed)
            {
                reader.Close();
            }
            return generalLedgerInfo;
        }

        public DataSet GetGeneralLedgerByQueryList(List<string> sqlWhere, GeneralLedgerQueryEntity generalledgerQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_GENERAL_LEDGER.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            if (!generalledgerQuery.IsGetAll)
            {
                temp = PagingHelper.GetPagingSQL(temp, generalledgerQuery.CurrentPage, generalledgerQuery.PageSize, generalledgerQuery.SortField, generalledgerQuery.SortDirection);
            }
            return DBHelper.ExecuteDataSet(CommandType.Text, temp);
        }

        public DataSet GetGeneralLedgerByQueryList(DataBase dataBase, DbTransaction tran, List<string> sqlWhere, GeneralLedgerQueryEntity generalledgerQuery, string whLoginID)
        {
            string temp = this.SQL_SELECT_ALL_GENERAL_LEDGER.Replace("@_@", whLoginID);
            for (int i = 0; i < sqlWhere.Count; i++)
            {
                if (i == 0)
                {
                    temp = temp + " WHERE " + sqlWhere[i].ToString();
                }
                else
                {
                    temp = temp + " AND " + sqlWhere[i].ToString();
                }
            }
            temp = PagingHelper.GetPagingSQL(temp, generalledgerQuery.CurrentPage, generalledgerQuery.PageSize, generalledgerQuery.SortField, generalledgerQuery.SortDirection);
            return dataBase.ExecuteDataSet(tran, CommandType.Text, temp);
        }

        public int InsertGeneralLedger(GeneralLedgerInfo generalLedgerInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                generalLedgerInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_GeneralLedger_Parameters(generalLedgerInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_GENERAL_LEDGER.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                conn.Close();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        public int InsertGeneralLedger(DataBase dataBase, DbTransaction tran, GeneralLedgerInfo generalLedgerInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                generalLedgerInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_GeneralLedger_Parameters(generalLedgerInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_INSERT_GENERAL_LEDGER.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }

        private SqlParameter[] Set_GeneralLedger_Parameters(GeneralLedgerInfo generalLedgerInfo)
        {
            SqlParameter[] paramArray = new SqlParameter[] { new SqlParameter(this.WH_ID, SqlDbType.VarChar, 30), new SqlParameter(this.GENERAL_LEDGER_ID, SqlDbType.VarChar, 20), new SqlParameter(this.GENERAL_LEDGER_NAME, SqlDbType.VarChar, 20), new SqlParameter(this.IS_ACTIVE, SqlDbType.VarChar, 1), new SqlParameter(this.REMARK, SqlDbType.VarChar, 1), new SqlParameter(this.CREATED_BY, SqlDbType.VarChar, 20), new SqlParameter(this.UPDATED_BY, SqlDbType.VarChar, 20) };
            paramArray[0].Value = generalLedgerInfo.WhID;
            paramArray[1].Value = generalLedgerInfo.GeneralLedgerID;
            paramArray[2].Value = generalLedgerInfo.GeneralLedgerName;
            if (!string.IsNullOrEmpty(generalLedgerInfo.IsActive))
            {
                paramArray[3].Value = generalLedgerInfo.IsActive;
            }
            else
            {
                paramArray[3].Value = DBNull.Value;
            }
            if (!string.IsNullOrEmpty(generalLedgerInfo.Remark))
            {
                paramArray[4].Value = generalLedgerInfo.Remark;
            }
            else
            {
                paramArray[4].Value = DBNull.Value;
            }
            paramArray[5].Value = generalLedgerInfo.CreatedBy;
            paramArray[6].Value = generalLedgerInfo.UpdatedBy;
            return paramArray;
        }

        public int UpdateGeneralLedger(GeneralLedgerInfo generalLedgerInfo, string whLoginID)
        {
            int result = 0;
            DataBase db = new DataBase();
            DbConnection conn = db.connection;
            conn.Open();
            DbTransaction tran = conn.BeginTransaction();
            try
            {
                generalLedgerInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_GeneralLedger_Parameters(generalLedgerInfo);
                if (paras != null)
                {
                    result += db.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_GENERAL_LEDGER.Replace("@_@", whLoginID), paras);
                }
                tran.Commit();
            }
            catch (SqlException sqlEx)
            {
                tran.Rollback();
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            finally
            {
                conn.Close();
            }
            return result;
        }

        public int UpdateGeneralLedger(DataBase dataBase, DbTransaction tran, GeneralLedgerInfo generalLedgerInfo, string whLoginID)
        {
            int result = 0;
            try
            {
                generalLedgerInfo.WhID = whLoginID.Substring(0, whLoginID.Length - 1);
                SqlParameter[] paras = this.Set_GeneralLedger_Parameters(generalLedgerInfo);
                if (paras != null)
                {
                    result += dataBase.ExecuteNonQuery(tran, CommandType.Text, this.SQL_UPDATE_GENERAL_LEDGER.Replace("@_@", whLoginID), paras);
                }
            }
            catch (SqlException sqlEx)
            {
                CommonErrorHandler.ErrorHandler(sqlEx, LogType.Error);
            }
            return result;
        }
    }
}

